import { IXMLAttr } from "./xmlUIParse/IXMLAttr";
import { IXMLData } from "./xmlUIParse/IXMLData";
import { IXMLTag } from "./xmlUIParse/IXMLTag";
import { XMLConfig } from "./xmlUIParse/XMLConfig";
import { XMLParser } from "./xmlUIParse/XMLParser";

const Path = CS.System.IO.Path;
export class Util
{
    public static packageDict:any;
    public static referencePkgs:string[];
    public static exportedList:any[];
    public static packInit():void
    {
        Util.packageDict = {};
        Util.referencePkgs = [];
        Util.exportedList = [];
    }
    /**使用示例：获取上两级目录
    *   string result = getParentDirectory("Assets/A/B/C/file.txt", 2);
    *   结果: "Assets/A/B" 
    **/
    public static getParentDirectory(path:string, levels:number = 1) {
        for (let i:number = 0; i < levels; i++) {
            path = Path.GetDirectoryName(path);
            if (path==null || path == "") break;
        }
        return path;
    }

    public static getFileName(path:string):string
    {
        let arr:string[] = path.split("/");
        let n:string;
        n = arr.pop();
        return n.split(".")[0];
    }
    /**是否需要导出代码(注意要确保packageDict字典已获得所有关联包，否则外部包描述获取不了则无法正确判定是否属于需要导出的数据)*/
    public static isExported(xmlData:IXMLData, packageData:IXMLData):boolean
    {        
        let descXMLData:IXMLData;
        descXMLData = Util.findDescXMLData(xmlData, packageData);
        if(descXMLData && descXMLData.attrs.exported)//需要导出代码
            return true;
        return false;
    }
    /**查询节点的描述对象*/
    public static findDescObj(xmlData:IXMLData):any
    {
        return Util.packageDict[xmlData.attrs.pkg];
    }
    /**查询节点的描述*/
    public static findDescXMLData(xmlData:IXMLData, packageData:IXMLData):IXMLData
    {
        let len:number;        
        let descXMLDatas:IXMLData[];
        packageData = xmlData.attrs.pkg?Util.packageDict[xmlData.attrs.pkg].packageData:packageData;        
        descXMLDatas = packageData.childs[0].childs;                
        len = descXMLDatas.length;
        while(--len>-1)
        {                    
            if(xmlData.fileName == descXMLDatas[len].attrs.name || xmlData.attrs.src == descXMLDatas[len].attrs.id)
                return descXMLDatas[len];
        }
        return null;
    }
    /**获取数据节点的类路径(注意要确保packageDict字典已获得所有关联包，否则外部包描述获取不了则无法得到正确的类路径)*/
    public static getClassPath(xmlData:IXMLData, packageData:IXMLData):string
    {        
        let descXMLData:IXMLData = Util.findDescXMLData(xmlData, packageData);
        if(descXMLData)
            return descXMLData.attrs.path.substr(1) + descXMLData.attrs.name.split(".")[0];
        return null;
    }
    /**获取定义类名
     * @param xmlData 节点数据
     * @param extentionXMLData 扩展数据
     * @param parser 解析器
     * @param descXMLDatas 描述列表数据
     * @param pkgName 包名
     * @param defaultCls 默认类名
    */
    public static getDefClass(xmlData:IXMLData,extentionXMLData:IXMLData,parser:XMLParser, packageData:IXMLData, pkgName:string, defaultCls:string):string
    {
        let cls:string;
        let descXMLData:IXMLData;
        if(extentionXMLData)
        {
            let ex:string = extentionXMLData.attrs.extention;
            if(ex == "Button")//按钮有3种模式，特殊处理一下类型
            {
                let mode:number;
                let attriDef:IXMLAttr = XMLConfig.getAttrDef(extentionXMLData.tagName, "mode");
                let result:IXMLData[] = parser.getTags(extentionXMLData, ["Button"]);
                if(result[0])
                {
                    mode = result[0].attrs.mode|0;
                    cls = attriDef.cls[mode];
                }
                else
                    mode = 0;
                cls = attriDef.cls[mode];
            }               
            else if(ex == null || ex == "Label")
            {
                if(Util.isExported(xmlData, packageData))
                {
                    let clsPath:string = Util.getClassPath(xmlData, packageData);
                    cls = pkgName + ".I" + clsPath;//自定义组件
                }                    
            }  
            else
            {
                let tagDef:IXMLTag = XMLConfig.tag[ex];
                if(tagDef)
                {
                    if(ex == "ScrollBar")//滚动条有两种，根据形状判断
                    {
                        let isHorizonal:boolean;
                        isHorizonal = extentionXMLData.attrs.width > extentionXMLData.attrs.height;
                        cls = tagDef.clsEnum[isHorizonal?1:0];                
                    }         
                    else
                        cls = tagDef.cls;//使用扩展名
                }                
            }
        }
        if(cls == null)
        {
            if(xmlData.tagName == "list")//列表分5种布局，所以有5个类
            {
                let layout:number;
                layout = xmlData.attrs.layout|0;
                let attriDef:IXMLAttr = XMLConfig.getAttrDef(xmlData.tagName, "layout");
                cls = attriDef.cls[layout];
            }
            else
                cls = XMLConfig.tag[xmlData.tagName].cls;//普通组件
        }
        return cls?cls:defaultCls;
    }
    public getFileName(descXMLdata:IXMLData):string
    {
        let attrs:any = descXMLdata.attrs;
        return attrs.path.substr(1) + attrs.name;
    }
}